import tkinter as tk
from tab_header import *
from tab_grid import *
from tkinter import *
from tkinter import simpledialog
from tools import *
from sys_input_box import *
from sys_sqlite3 import *
from sys_config import *
from project_info import *
from sys_project import *
from sys_image import *
from sys_status_bar import *
# import time

class MainWindow():
    def __init__(self):
        self.project_info = ProjectInfo().conf()
        self.tools      = Tools()
        self.project    = SysProject()
        self.status_bar = SysStatusBar()

    def init(self):
        """初始化窗口，成功返回ture
        """
        self.config = SysConfig()
        if(self.config.init()):
            print(self.config.value('database', 'file'))
        else:
            return False
        # 数据库
        self.db_file = self.config.value('database', 'file', '', True)
        if len(self.db_file) == 0:
            self.tools.msg("未配置数据库文件:database-file")
            return False
        if self.tools.file_exist(self.db_file, True) == False: return False
        
        # 创建Tkinter窗口
        window_width = 1000
        self.window = tk.Tk()
        self.window.title(self.project_info['name'])
        self.window.geometry(self.tools.get_geometry(self.window, window_width, 560))
        # window.attributes('-fullscreen', True)  #全屏
        # window.wm_state('zoomed') #最大化
        self.window.resizable(True, True)
        self.window.bind("<Configure>", self.on_window_configure)  # 绑定窗口大小改变事件
        self.window.bind("<Visibility>", self.on_window_configure) 
        self.window.bind("<FocusIn>", self.on_focus_in) 
        self.window.bind("<FocusOut>", self.on_focus_out) 

        # 显示图片
        image_file = self.config.value('ui', 'bg_image')
        print("image_file", image_file)
        self.sys_image = False
        if len(image_file) > 0:
            if self.tools.file_exist(image_file):
                self.sys_image = SysImage()
                self.sys_image.load(self.window, image_file, True)
            else:
                self.tools.msg(f"文件不存在:{image_file}")

        # 菜单与状态栏
        self.project.load_menu(self.window)
        self.status_bar.gen_status_bar(self.window, '50,30,30,50', self.on_status_bar_click, self.on_status_bar_dbl_click)
        # self.status_bar.set_text(0, f"版本:{self.project_info['version']}")
        self.status_bar.set_all_text([
            f"数据库:{self.db_file}",
            f"版本:{self.project_info['version']}",
            f"登录用户:",
            f"{self.project_info['name']}"
            ])
        
        # 计算平均字符宽度用
        # font_size = self.config.value('ui', 'font_size', 12)
        # first_label = tk.Label(self.window, text='first_label', anchor='w', width=100, font=('宋体', font_size)).pack(side='left', padx=5, pady=5)

        # self.window.after(1000, lambda: self.after_sleep(first_label))
        # 运行Tkinter事件循环
        self.window.mainloop()

        return True
    
    def on_status_bar_click(self, index):
        print("on_status_bar_click", index)
    def on_status_bar_dbl_click(self, index):
        print("on_status_bar_dbl_click", index)
        if index == 3:
            self.project.show_about()

    def on_window_configure(self, event):
        self.window_adjust()

    def on_focus_in(self, event):
        print("窗口激活")
        # self.project.get_char_width()

    def on_focus_out(self, event):
        print("窗口失去焦点")

    def window_adjust(self):
        """根据窗口大小调整内部组件的位置和大小
        """
        # 获取宽度和高度
        # screen_width = self.window.winfo_screenwidth()
        # screen_height = self.window.winfo_screenheight()
        win_width   = self.window.winfo_width()
        win_height  = self.window.winfo_height()
        # print("window_adjust", f"{win_width},{win_height}")
        if self.sys_image: self.sys_image.resize(win_width, win_height - 25)
        self.status_bar.adjust(win_width, win_height)
        
        # print(f"screen:{win_width},{win_height}")
        _right  = 100
        _left   = win_width - _right
        if _left < 1:
            return

    def after_sleep(self, lbl:tk.Label):
        print("after_sleep")
        w = self.project.get_char_width()
        print("计算平均字符宽度", w)

if __name__ == "__main__":
    main = MainWindow()
    main.init()